
package com.tools.cleanmaster.ui.view.header;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.nineoldandroids.view.ViewHelper;
import com.tools.cleanmaster.R;

@SuppressWarnings("static-access")
public final class Header {
    private View mRoot;

    private ImageView mLogo, mNewLabel;

    private TextView mTitle;

    private ImageButton mSettingButton;

    private ImageButton mTriggerButton;

    private ImageButton mDuGroup;

    private OnBackStackListener mBackListener;

    private View.OnClickListener mSettingButtonClickListener;

    private View.OnClickListener mTriggerButtonClickListener;

    private View.OnClickListener mDuGroupClickListener;

    public static Header create(Activity activity, int titleId) {
        return new Header(activity, titleId);
    }

    public static Header create(View view, int titleId) {
        return new Header(view, titleId);
    }

    private Header(Activity activity, int titleId) {
        mRoot = activity.findViewById(titleId);
        mLogo = mRoot.findViewById(R.id.logo);
        mNewLabel = mRoot.findViewById(R.id.settings_indicator);
        mLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBackListener != null) {
                    mBackListener.onBackStack();
                }
            }
        });
        mTitle = mRoot.findViewById(R.id.title);

        mSettingButton = mRoot.findViewById(R.id.settings);
        mSettingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSettingButtonClickListener != null) {
                    mSettingButtonClickListener.onClick(v);
                }
            }
        });

        mTriggerButton = mRoot.findViewById(R.id.trigger);
        mTriggerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTriggerButtonClickListener != null) {
                    mTriggerButtonClickListener.onClick(v);
                }
            }
        });
    }

    private Header(View view, int titleId) {
        mRoot = view.findViewById(titleId);
        mLogo = mRoot.findViewById(R.id.logo);
        mLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBackListener != null) {
                    mBackListener.onBackStack();
                }
            }
        });

        mTitle = mRoot.findViewById(R.id.title);

        mSettingButton = mRoot.findViewById(R.id.settings);
        mSettingButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSettingButtonClickListener != null) {
                    mSettingButtonClickListener.onClick(v);
                }
            }
        });

        mTriggerButton = mRoot.findViewById(R.id.trigger);
        mTriggerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mTriggerButtonClickListener != null) {
                    mTriggerButtonClickListener.onClick(v);
                }
            }
        });
    }

    public void setNewLabel(boolean has) {
        mNewLabel.setImageResource(has ? R.drawable.icon_pop_new_tip_small : 0);
    }

    public Header setTitle(CharSequence title) {
        mTitle.setText(title.toString());
        return this;
    }

    public Header setTitle(int titleRes) {
        mTitle.setText(mRoot.getContext().getString(titleRes));
        return this;
    }

    public Header setLogo(int logoResId) {
        return setLogo(logoResId, null);
    }

    public Header setLogo(int logoResId, OnBackStackListener listener) {
        if (logoResId > 0) {
            mLogo.setVisibility(View.VISIBLE);
            mLogo.setImageResource(logoResId);
        } else {
            mLogo.setVisibility(View.GONE);
        }

        mBackListener = listener;

        return this;
    }

    public Header setBackListener(final OnBackStackListener listener) {
        if (listener != null) {
            showLogo();
        }
        mBackListener = listener;
        return this;
    }

    public Header setTitleLeftAlign() {
        mTitle.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
        return this;
    }

    public Header setSettingButton(int resid, OnClickListener listener) {
        if (resid > 0) {
            mSettingButton.setImageResource(resid);
            mSettingButton.setVisibility(View.VISIBLE);
        } else {
            mSettingButton.setVisibility(View.INVISIBLE);
        }

        mSettingButtonClickListener = listener;

        return this;
    }

    public Header setTriggerButton(int resid, OnClickListener listener) {
        if (resid > 0) {
            mTriggerButton.setImageResource(resid);
            mTriggerButton.setVisibility(View.VISIBLE);
        } else {
            mTriggerButton.setVisibility(View.INVISIBLE);
        }

        mTriggerButtonClickListener = listener;
        return this;
    }

    public Header hideLogo() {
        mLogo.setVisibility(View.GONE);
        return this;
    }

    public Header showLogo() {
        mLogo.setVisibility(View.VISIBLE);
        return this;
    }

    public Header hideSettingButton() {
        mSettingButton.setVisibility(View.GONE);
            mNewLabel.setVisibility(View.GONE);
        return this;
    }

    public void setVisibility(int visibility) {
        mRoot.setVisibility(visibility);
    }

    public void setAlpha(float alpha) {
        ViewHelper.setAlpha(mRoot, alpha);
    }

    public void setBackgroundColor() {
        if (mRoot != null) {
            mRoot.setBackgroundResource(R.color.common_background_blue);
        }
    }
}
